﻿using DataModels.Dto;
using DataModels.Entities.Base;
using DataModels.Entities.House;
using SqlSugar;

namespace Services.Base
{
    public class ProjectService(SqlSugarClient db) : BaseService(db)
    {
        // 获取项目信息
        public ResponseModel GetProject()
        {
            //查询当前用户关联的房屋信息
            List<int?> sigeid = GetCustIdListBySIGE();
            var res = db.Queryable<CustomerHouseRelation>().Where(x => sigeid.Contains(x.CustId) && (x.CustStatus == "1000" || x.CustStatus == "1001")).ToList();
            var projectIds = new List<string>();
            if (res.Count > 0)
            {
                projectIds = [.. res.Select(x => x.OrganId).Distinct()];
            }
            var list = db.Queryable<Projects>().WhereIF(projectIds.Count > 0, x => projectIds.Contains(x.CommunityId)).ToList();
            return Success(list);
        }


        // 获取房产信息
        public ResponseModel GetProperties(Guid ProjectId)
        {
            return Success(db.Queryable<Properties>().Where(pp => pp.ProjectUUID == ProjectId).ToList());
        }

        /// <summary>
        /// 获取全部项目列表
        /// </summary>
        /// <returns></returns>
        public ResponseModel GetProjectList()
        {
            var properties = db.Queryable<Projects>().ToList();
            return Success(properties);
        }

        /// <summary>
        /// 获取楼栋信息
        /// </summary>
        /// <returns></returns>
        public ResponseModel GetBuilding(string? ProjectUUID)
        {
            var query = db.Queryable<Building>();

            if (!string.IsNullOrEmpty(ProjectUUID))
            {
                var organIdStr = GetOrganIdBySIGE(Guid.Parse(ProjectUUID));
                if (int.TryParse(organIdStr, out int organId))
                {
                    query = query.Where(x => x.OrganId == organId);
                }
            }

            var buildings = query.OrderBy(x => x.BuildId).ToList();

            return Success(buildings);
        }

        /// <summary>
        /// 获取单元信息
        /// </summary>
        /// <param name="BuildingUUID"></param>
        /// <returns></returns>
        public ResponseModel GetUnitByBuilding(string BuildingUUID, string? ProjectUUID)
        {
            var BuildId = GetBuildBySIGE(Guid.Parse(BuildingUUID));

            int organId = 0;
            if (!string.IsNullOrEmpty(ProjectUUID))
            {
                organId = int.Parse(GetOrganIdBySIGE(Guid.Parse(ProjectUUID)));
            }
            var Units = db.Queryable<Unit>().Where(x => x.buildId == BuildId).WhereIF(organId > 0, x => x.organId == organId).ToList();
            return Success(Units);
        }

        /// <summary>
        /// 获取单元楼层信息
        /// </summary>
        /// <param name="UnitUUID"></param>
        /// <returns></returns>
        public ResponseModel GetFloorByUnit(string UnitUUID, string? ProjectUUID)
        {
            var unitId = GetUnitBySIGE(Guid.Parse(UnitUUID));

            int organId = 0;
            if (!string.IsNullOrEmpty(ProjectUUID))
            {
                organId = int.Parse(GetOrganIdBySIGE(Guid.Parse(ProjectUUID)));
            }
            var floors = db.Queryable<Floor>().Where(x => x.UnitId == unitId).WhereIF(organId > 0, x => x.CommunityId == organId).OrderBy(x => x.FloorId).ToList();
            floors.ForEach(x =>
            {
                x.ProjectUUID = GetProjectUUIDBySIGE((int)x.CommunityId);
            });
            return Success(floors);
        }

        /// <summary>
        /// 获取对应楼层的房号信息
        /// </summary>
        /// <param name="FloorUUID"></param>
        /// <param name="ProjectUUID"></param>
        /// <returns></returns>
        public ResponseModel GetRoomNoByFloor(string FloorUUID, string ProjectUUID)
        {
            int floorId = GetFloorBySIGE(FloorUUID);
            int organId = int.Parse(GetOrganIdBySIGE(Guid.Parse(ProjectUUID)));

            var hosues = db.Queryable<HousingBasic>().Where(h => h.Floor_id == floorId && h.OrganId == organId).ToList();
            return Success(hosues);
        }

        /// <summary>
        /// 获取房号信息根据楼栋UUID
        /// </summary>
        /// <param name="BuildUUID"></param>
        /// <param name="ProjectUUID"></param>
        /// <returns></returns>
        public ResponseModel GetRoomNoByBuildUUID(string BuildUUID, string ProjectUUID)
        {
            int BuildId = GetBuildBySIGE(Guid.Parse(BuildUUID));
            int organId = int.Parse(GetOrganIdBySIGE(Guid.Parse(ProjectUUID)));

            var hosues = db.Queryable<HousingBasic>().Where(h => h.BuildId == BuildId && h.OrganId == organId).ToList();
            return Success(hosues);
        }


    }
}